<template>
  <div class="goods-times-container">
    <!-- 面包屑 -->
    <div style="width: auto;height: 60px;font-size: larger;">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/Main' }">仪表台</el-breadcrumb-item>
        <el-breadcrumb-item>货品保质期管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!-- 数据容器 -->
    <div class="data-container">
      <!-- 查询区域 -->
       <div class="search-section">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="货品">
            <el-input v-model="formInline.Goodsname" placeholder="请输入货品名称" clearable />
          </el-form-item>
          <el-form-item label="剩余天数">
            <el-input v-model="formInline.nums1" placeholder="请输入" clearable style="width: 80px;"/>--
            <el-input v-model="formInline.nums2" placeholder="请输入" clearable style="width: 80px;"/>&ensp;天
          </el-form-item>
          
          <el-form-item label="货品条码">
            <el-input v-model="formInline.Goodsbarcodes" placeholder="请输入货品条码" clearable />
          </el-form-item>
          <el-form-item label="货品类型">
               <el-select v-model="formInline.GoodstyoeName" placeholder="请选择货品类型" style="width: 240px"
          @change="GetSelectTypes()">
          <el-option v-for="item in goodsTypes" :label="item.wGoodstypename" :value="item.goodstypecoding" />
        </el-select>
          </el-form-item>
          <el-form-item label="单位">
            <el-select v-model="formInline.GoodsUnitName" placeholder="请选择单位" style="width: 240px"
          @change="GetSelectUnits()">
          <el-option v-for="item in Units" :label="item.unitName" :value="item.unitscoding" />
        </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="formInline.Status" placeholder="请选择" clearable style="width: 100px;">
              <el-option label="正常" value="正常" />
              <el-option label="即将到期" value="即将到期" />
              <el-option label="已到期" value="已到期" />
            </el-select>
          </el-form-item>
          <el-form-item label="仓库名称">
           <el-select v-model="formInline.WarehouseNo" placeholder="请选择所属仓库" style="width: 240px"
          @change="GetSelectWareHouse()">
          <el-option v-for="item in wareHouseOptions" :label="item.warename" :value="item.warehouseNo" />
        </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表格区域 -->
      <div class="table-section">
        <el-table 
          :data="tableData" 
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column type="index" label="序号" width="80" />
          <el-table-column prop="goodscoding" label="货品编号" width="150">
            <template #default="scope">
              <el-link 
                type="primary" 
                @click="goToGoodsDetails(scope.row)"
                style="cursor: pointer;"
              >
                {{ scope.row.goodscoding }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column prop="goodsname" label="货品名称" width="150" />
          <el-table-column prop="goodstyoeName" label="货品类型" width="120" />
          <el-table-column prop="goodsUnitName" label="单位" width="120" />
          <el-table-column prop="goodsspecifications" label="规格" width="120" />
          <el-table-column prop="status" label="状态" width="120"/>
          <el-table-column prop="warename" label="仓库名称" width="100" />
          <el-table-column prop="locationName" label="仓位名称" width="100" />
          <el-table-column prop="goodsbarcodes" label="货品条码" width="180" />
          <el-table-column prop="num" label="当前库存" width="80" />
          <el-table-column prop="productionTime" label="生产日期" width="100">
            <template #default="scope">
              {{ formatDate(scope.row.productionTime) }}
            </template>
          </el-table-column>
          <el-table-column prop="shelflife" label="保质期" width="80"/>
          <el-table-column prop="days" label="剩余天数" width="80"/>
          <el-table-column prop="earlywarning" label="预警天数" width="80" />
        </el-table>
      </div>
      <!-- 分页 -->
    <table>
            <tr>
                <th>
                    <el-pagination v-model:current-page="formInline.PageIndex" v-model:page-size="formInline.PageSize"
                        :page-sizes="[10, 20, 30, 40, 50]" layout="slot, sizes" :total="formInline.count">
                        当前第{{ formInline.PageIndex }}页/共{{ formInline.pcount}}页/共{{
                            formInline.count 
                        }}条
                    </el-pagination>
                </th>
                <th>
                    <el-pagination v-model:current-page="formInline.PageIndex" v-model:page-size="formInline.PageSize"
                        :page-sizes="[10, 20, 30, 40, 50]" layout="slot, prev, pager, next" :total="formInline.count">
                        <el-button :disabled="formInline.PageIndex == 1" @click="formInline.PageIndex = 1"
                            type="primary">首页</el-button>
                    </el-pagination>
                </th>
                <th>
                    <el-pagination v-model:current-page="formInline.PageIndex" v-model:page-size="formInline.PageSize"
                        :page-sizes="[10, 20, 30, 40, 50]" layout="slot,jumper" :total="formInline.count">
                        <el-button :disabled="formInline.PageIndex == formInline.pcount" @click="formInline.PageIndex = formInline.PageSize"
                            type="primary">尾页</el-button>
                    </el-pagination>
                </th>
            </tr>
        </table>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import service from '@/utils/request';
import { ElMessage } from 'element-plus';

const router = useRouter();

onMounted(()=>{
  GetSelectWareHouse()
  GetSelectUnits()
  GetSelectTypes()
})
// 表单数据
const formInline = reactive({
  PageIndex: 1,
  PageSize: 10,
  Goodsname: '',
  GoodstyoeName: '',
  GoodsUnitName: '',
  Status: '',
  Goodsbarcodes: '',
  WarehouseNo: '',
  nums1: '',
  nums2: '',
  count:0,
  pcount:0
 
});

// 表格数据
const tableData = ref<any[]>([]);
const totalCount = ref(0);
const selectedRows = ref<any[]>([]);

// 获取数据
const getData = async () => {
  try {
    service.get('/api/app/standing__Book/goods-times',{params:{ PageIndex: formInline.PageIndex,
        PageSize: formInline.PageSize,
        Goodsname: formInline.Goodsname,
        GoodstyoeName: formInline.GoodstyoeName,
        GoodsUnitName: formInline.GoodsUnitName,
        Status: formInline.Status,
        Goodsbarcodes: formInline.Goodsbarcodes,
        WarehouseNo: formInline.WarehouseNo,
        nums1: formInline.nums1,
        nums2: formInline.nums2}}).then(res=>{
          tableData.value = res.data.pageDatas;
          formInline.count=res.data.totalData
         formInline.pcount=res.data.totalPage
        })
   
  } catch (error) {
    console.error('获取数据失败:', error);
    ElMessage.error('获取数据失败');
  }
};

// 查询
const onSubmit = () => {
  formInline.PageIndex = 1;
  getData();
};

// 重置表单
const resetForm = () => {
  Object.assign(formInline, {
    PageIndex: 1,
    PageSize: 10,
     Goodsname: '',
  GoodstyoeName: '',
  GoodsUnitName: '',
  Status: '',
  Days: '',
  Goodsbarcodes: '',
  WarehouseNo: '',
  nums1: '',
  nums2: ''
  });
  getData();
};

//仓库
const wareHouseOptions = ref([
  {
    warename: "",
    warehouseNo: "",
  },
]);
const GetSelectWareHouse = () => {
  service
    .get(
      "/api/app/standing__Book/select-ware-house"
    )
    .then((res) => {
      if (res.data.code == 200) {
        wareHouseOptions.value = res.data.resultData;
      }
    });
};
//单位
const Units = ref([
  {
    unitscoding: "",
    unitName: "",
  },
]);
const GetSelectUnits = () => {
  service
    .get(
      "/api/app/goods/goods-unit-list"
    )
    .then((res) => {
    
        Units.value = res.data
    });
};
//类型
const goodsTypes = ref([
  {
    goodstypecoding: "",
    wGoodstypename: "",
  },
]);
const GetSelectTypes = () => {
  service
    .get(
      "/api/app/goods/goods-type-list"
    )
    .then((res) => {
     
        goodsTypes.value = res.data;
    });
};

// 跳转到商品详情页面
const goToGoodsDetails = (row: any) => {
  router.push({
    path: '/GetGoods_Details',
    query: {
      id: row.goodscoding
    }
  });
};



// 选择变化
const handleSelectionChange = (selection: any[]) => {
  selectedRows.value = selection;
};

// 分页大小变化
const handleSizeChange = (val: number) => {
  formInline.PageSize = val;
  formInline.PageIndex = 1;
  getData();
};

// 当前页变化
const handleCurrentChange = (val: number) => {
  formInline.PageIndex = val;
  getData();
};

// 格式化日期
const formatDate = (dateStr: string) => {
  if (!dateStr) return '';
  const date = new Date(dateStr);
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  }).replace(/\//g, '-');
};


onMounted(() => {
  getData();
});
</script>

<style scoped>
.goods-times-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #e3f0ff 0%, #f8fbff 100%);
  padding: 20px;
}

/* 数据容器样式 */
.data-container {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  margin-bottom: 20px;
}

/* 查询区域样式 */
.search-section {
  padding: 20px;
  border-bottom: 1px solid #f0f2f5;
  background: #fafbfc;
}

.search-section .demo-form-inline {
  margin: 0;
}

/* 操作按钮区域 */
.action-section {
  padding: 15px 20px;
  background: #fafbfc;
  border-bottom: 1px solid #f0f2f5;
  display: flex;
  gap: 10px;
}

/* 表格区域样式 */
.table-section {
  padding: 0;
}

.table-section .el-table {
  margin: 0;
  border-radius: 0;
  box-shadow: none;
}

/* 分页区域样式 */
.pagination-section {
  padding: 15px 20px;
  background: #fafbfc;
  border-top: 1px solid #f0f2f5;
  display: flex;
  justify-content: center;
}

.pagination-section .el-pagination {
  margin: 0;
  background: transparent;
  box-shadow: none;
  padding: 0;
}

/* 剩余天数样式 */
.days-normal {
  color: #67c23a;
  font-weight: bold;
}

.days-warning {
  color: #e6a23c;
  font-weight: bold;
}

.days-expired {
  color: #f56c6c;
  font-weight: bold;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .search-section .el-form-item {
    margin-bottom: 10px;
  }
}

@media (max-width: 768px) {
  .goods-times-container {
    padding: 10px;
  }
  
  .search-section {
    padding: 15px;
  }
  
  .action-section {
    padding: 10px 15px;
  }
}
</style>
